﻿package net.wangit.framework.session;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.context.LocalContext;
import net.wangit.jdbc.table.Column;
import net.wangit.jdbc.table.TableMeta;
import net.wangit.jdbc.table.TableMetaFactory;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.OrderElement;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;
import net.wangit.view.unit.component.QueryUnit;
import net.wangit.view.unit.util.UnitUtil;


public class UnitDataHelper {

	public UnitDataHelper() {
	}

	public static void setMemoryData(ActionContext ac) {
		String units[] = ac.getRequestParameters("_ru_");
		if (units == null)
			return;
		AgileSession session = ActionContextHelper.getSession(ac);
		for (int i = 0; i < units.length; i++) {
			String parameters[] = StringUtil.split(units[i], ",");
			UnitComponent uc = UnitFactory.getUnitWithoutClone(parameters[0],
					parameters[1]);
			MapList data = getDataFromContext(ac, uc, parameters[1]);
			UnitSettings us = session.getUnitSettings(UnitUtil.getKey(
					parameters[0], parameters[1]));
			us.setData(data);
		}

	}

	public static MapList getMemoryData(UnitComponent uc) {
		AgileSession session = ActionContextHelper.getSession(LocalContext
				.getLocalContext().getActionContext());
		UnitSettings us = session.getUnitSettings(uc.getDomain(), uc.getId());
		return us.getData();
	}

	public static void setSnapshot(ActionContext ac) {
		String units[] = ac.getRequestParameters("_u_");
		if (units == null)
			return;
		Map<String, MapList> map = new HashMap<String, MapList>();
		for (int i = 0; i < units.length; i++) {
			String parameters[] = StringUtil.split(units[i], ",");
			String domain = parameters[0];
			String unitId = parameters[1];
			UnitComponent uc = UnitFactory.getUnitWithoutClone(domain, unitId);
			if (uc.isRecovery()) {
				MapList data = getDataFromContext(ac, uc, unitId);
				map.put(units[i], data);
			}
		}

		AgileSession session = ActionContextHelper.getSession(ac);
		session.setSnapshots(map);
	}

	private static MapList getDataFromContext(ActionContext ac,
			UnitComponent uc, String unitId) {
		MapList data = new MapList();
		boolean isQueryUnit = uc instanceof QueryUnit;
		for (int i = 0; i < uc.size(); i++) {
			ElementComponent ec = (ElementComponent) uc.getElement(i);
			setValue(ac, data, ec, isQueryUnit);
		}

		if (uc.getListSelectMode() != 0) {
			String selectElementId = (new StringBuilder("_s_")).append(unitId)
					.toString();
			setValue(ac, data, selectElementId, selectElementId);
		}
		if (!isQueryUnit) {
			setValue(ac, data, "", UnitUtil.getOperator(unitId));
			String tableName = uc.getTableName();
			if (!Checker.isEmpty(tableName)) {
				TableMeta meta = TableMetaFactory.getTableMeta(uc.getDomain(),
						tableName);
				List<Column> columns = meta.getPrimaryKey().getColumns();
				for (int i = 0; i < columns.size(); i++) {
					String name = ((Column) columns.get(i)).getName();
					String id = (new StringBuilder(String.valueOf(name)))
							.append(".k").toString();
					String key = (new StringBuilder(String.valueOf(unitId)))
							.append(".").append(id).toString();
					setValue(ac, data, id, key);
				}

				Column versionColumn = meta.getVersionColumn();
				if (versionColumn != null) {
					String name = versionColumn.getName();
					String id = (new StringBuilder(String.valueOf(name)))
							.append(".v").toString();
					String key = (new StringBuilder(String.valueOf(unitId)))
							.append(".").append(id).toString();
					setValue(ac, data, id, key);
				}
			}
		}
		return data;
	}

	private static void setValue(ActionContext ac, MapList data,
			ElementComponent ec, boolean isQueryUnit) {
		String id = ec.getId();
		String key = ElementUtil.getAlias(ec);
		setValue(ac, data, id, key);
		if (isQueryUnit) {
			int queryMode = ec.getQueryMode();
			if (queryMode == 4)
				setValue(ac, data, (new StringBuilder(String.valueOf(id)))
						.append(".t").toString(), (new StringBuilder(String
						.valueOf(key))).append(".t").toString());
			else if (queryMode == 3)
				setValue(ac, data, (new StringBuilder(String.valueOf(id)))
						.append(".p").toString(), (new StringBuilder(String
						.valueOf(key))).append(".p").toString());
			if (ec instanceof OrderElement)
				setValue(ac, data, (new StringBuilder(String.valueOf(id)))
						.append(".o").toString(), (new StringBuilder(String
						.valueOf(key))).append(".o").toString());
		}
	}

	private static void setValue(ActionContext ac, MapList data, String id,
			String key) {
		String values[] = ac.getRequestParameters(key);
		if (values != null) {
			for (int i = 0; i < values.length; i++)
				data.put(i, id, values[i]);

		}
	}
}
